package net.tarine.gastronomi.server.persistence;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.tarine.gastronomi.server.model.ForumThread;
import net.tarine.gastronomi.server.model.Gas;

public class ForumDao {

	// thread
	@SuppressWarnings("unchecked")
	public static List<ForumThread> findForumThreads(PersistenceManager pm,
			Gas gas, int offset, int pageSize) {
		Query query = pm.newQuery(ForumThread.class);
		query.setFilter("gas == GAS");
		query.setOrdering("modifiedDt ascending");
		query.declareParameters("Gas GAS");
		query.setRange(offset, offset + pageSize);
		List<ForumThread> results = (List<ForumThread>) query.execute(gas);
		return results;
	}

	// public static void loadUnownedRelationships(PersistenceManager pm,
	// ForumThread thread) {
	// if (thread == null) return;
	// GasUser gasUser = pm.getObjectById(GasUser.class, thread.getGasUserId());
	// thread.setGasUserTO(pm.detachCopy(gasUser));
	// for (ForumMessage fm:thread.getMessageList()) {
	// loadUnownedRelationships(pm, fm);
	// }
	// }
	//
	// public static void loadUnownedRelationships(PersistenceManager pm,
	// ForumMessage msg) {
	// if (msg == null) return;
	// GasUser gasUser = pm.getObjectById(GasUser.class, msg.getGasUserId());
	// msg.setGasUserTO(pm.detachCopy(gasUser));
	// }

	/**
	 * PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx =
	 * pm.currentTransaction(); try { tx.begin(); // Start the PM transaction
	 * 
	 * ... perform some persistence operations
	 * 
	 * tx.commit(); // Commit the PM transaction } finally { if (tx.isActive())
	 * { tx.rollback(); // Error occurred so rollback the PM transaction } }
	 */
}
